<?php
namespace modules\post;
use \phiction\exceptions\no_such_page;
use \modules\comment\page as comment;
use \modules\auth\api as auth;
use \modules\ui;

class page
{
    static function mapping()
    {
        return [
            // redirections
            '/' => '/post/list/1',
            '/list' => '/post/list/1',
            '/add' => 'add',
            '/#id' => '/post/#id/1',

            // mapping
            '/list/#page' => 'list',
            '/#id/edit' => 'edit',
            '/#id/#comment-page' => 'fetch',
        ];
    }

    static function list($q, $html, &$args)
    {
        $page = $args['page'];

        $posts = api::list($q, $args);

        $list = ['div', 'class' => 'post-list'];
        if (count($posts) === 0) {
            $list[] = ['div', 'class' => 'not-exist', "没有文章"];
        } else {
            foreach ($posts as $post)
                $list[] = self::list_item($html, $post);

            $page_link = function ($i) { return "/post/list/$i"; };
            $has_next = (count($posts) === 10);
            $list[] = ui::pager($page_link, $page, $has_next);
        }

        $args['SUBTITLE'] = "文章";
        $args['HEADER-CONTROL'] = ui::header_control_or_login($q, $html, $args, [
            [ui::icon_label('撰写新文章', 'plus'), '/post/add'],
        ]);
        return $html->mark($list);
    }

    static function fetch($q, $html, &$args)
    {
        $id = $args['id'];
        $comment_page = $args['comment-page'];

        # get post
        $post = api::fetch($q, $args);
        if (is_null($post)) throw new no_such_page("post not found: $id");

        # get comments
        $args['post'] = $id;
        $args['page'] = $comment_page;
        $args['page-link'] = function ($i) use ($id) { return "/post/$id/$i"; };
        $comments = comment::list($q, $html, $args);

        # compose the page
        $args['SUBTITLE'] = $post['title'];
        $args['HEADER-CONTROL'] = ui::header_control_or_login($q, $html, $args, [
            [ui::icon_label('编辑', 'edit'), "/post/$id/edit"],
            [ui::icon_label('删除', 'trash'), 'class' => 'delete-post', 'data-id' => $id],
        ]);
        return $html->mark(['.',
            [ 'a', 'class' => 'post-title block', $post['title'], 'href' => "/post/$id" ],
            [ 'div', 'class' => 'post-author', $post['author'] ],
            [ 'div', 'class' => 'post-body', $post['body'] ],
            $comments,
        ]);
    }

    static function add($q, $html, &$args)
    {
        auth::validate_with_role($q, $args, 'admin');
        $args['SUBTITLE'] = "撰写文章";
        return ui::form($html, '/api/post/add',
            ui::icon_label('发表', 'edit'), [
                ['title', 'text', '标题'],
                ['body', 'textarea', '正文', 'rows' => 20],
            ], [
                [ui::icon_label('取消', 'angle-left'), '/'],
            ], '/');
    }

    static function edit($q, $html, &$args)
    {
        auth::validate_with_role($q, $args, 'admin');

        $id = $args['id'];
        $post = api::fetch($q, $args);
        if (is_null($post)) throw new no_such_page("post not found: $id");

        $title = $post['title'];
        $body = $post['body'];

        $args['SUBTITLE'] = "编辑 $title";
        $redirect = "/post/$id";
        return ui::form($html, '/api/post/edit',
            ui::icon_label('编辑', 'edit'), [
                ['title', 'text', '标题', $title],
                ['body', 'textarea', '正文', $body, 'rows' => 20],
                ['id', 'hidden', $id],
            ], [
                [ui::icon_label('取消', 'angle-left'), $redirect],
            ], $redirect);
    }


    private static function list_item($html, $post)
    {
        $id = $post['id'];
        $title = $post['title'];
        $author = $post['author'];
        return $html->mark([
            'div', 'class' => 'post-list-item',
            [ 'a', 'href' => "/post/$id", 'class' => 'post-list-item-title block', $title ],
            [ 'div', 'class' => 'post-list-item-author', $author ],
        ]);
    }
}

